#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

int count = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* Count(void* args)
{
    pthread_detach(pthread_self());
    uint64_t number = (uint64_t)args;
    cout << "pthread: " << number << " create success" << endl;

    while(true)
    {
        pthread_mutex_lock(&lock);

        pthread_cond_wait(&cond, &lock);
        std::cout << "pthread: " << number << " , cnt: " << count++ << std::endl;

        pthread_mutex_unlock(&lock);
    }

    return nullptr;
}

int main()
{
    for(uint64_t i = 1; i <= 5; i++)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, Count, (void*)i);
        usleep(1000);
    }
    sleep(2);
    cout << "main thraed ctrl begin: " << endl;

    while(true)
    {
        sleep(1);
        pthread_cond_signal(&cond);
        cout << "signal one thread ..." << endl;
    }

    return 0;
}